package it.unina.ingswdemo.persistence;

import it.unina.ingswdemo.beans.Image;

import java.util.List;
import java.util.logging.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@ManagedBean(name="retrieveImages")
@RequestScoped
public class RetrieveUserImages {
	
	List<Key> imageKeys;
	String headerMessage;
	
	public RetrieveUserImages(){
		
			imageKeys=retrieveImageKeys();
	}
	
	
	private List<Key> retrieveImageKeys() {
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
	    PersistenceManager pm = PMF.get().getPersistenceManager();

	    Query q = pm.newQuery("select key from " + Image.class.getName()+ " where user == userParam");
	    q.declareParameters("String userParam");
//	    Query q = pm.newQuery(Image.class);
	    
	    
	  
	    try {
	    	
		    List ids = (List) q.execute(user.getNickname());
//		    System.out.print(ids);
		    return ids;
	        
	    } catch(Exception e){
	    	System.out.println(e.getClass()+" query="+q);
	    	return null;
	    }finally {
	    	
	        q.closeAll();

	    }

	    
	}


	public List<Key> getImageKeys() {
		return imageKeys;
	}


	public void setImageKeys(List<Key> imageKeys) {
		this.imageKeys = imageKeys;
	}
	
	public String getHeaderMessage(){
		
		if(imageKeys!=null && !imageKeys.isEmpty())
			return "Immagini caricate";
		
		return "Nessun immagine caricata";
		
	}

}
